
// 978.最长湍流子数组
class Solution {
public:
    int maxTurbulenceSize(vector<int>& nums) {
        // 使用动态规划实现
        int n = nums.size();
        if(n < 2) return 1;
        vector<int> f(n,1);
        f[1] = (nums[1]==nums[0]? 1 :2 );
        int ret = f[1];
        for(int i = 2; i < n ;i++)
        {
            long long a = nums[i-1] - nums[i-2] , b = nums[i] - nums[i-1];
            if(a*b < 0) f[i] = f[i-1] + 1;
            else if(a*b > 0) f[i] = 2;
            else 
            {
                if(b == 0) f[i] = 1;
                else f[i] = 2;
            }

            ret = max(ret,f[i]);
        }       
        return ret;
    }
};